<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="css/pintuer.css">
    <link rel="stylesheet" href="css/admin.css">
    <script src="js/jquery.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <script src="js/pintuer.js"></script>
    <script src="js/commons1.js"></script>

</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head"><strong><span class="icon-key"></span> 修改会员密码</strong></div>
    <div class="body-content">
        <form method="post" class="form-x" action="">
            <div class="form-group">
                <div class="label">
                    <label id="zh">管理员帐号：</label>
                </div>
                <div class="field">
                    <label id="adminLoginName" style="line-height:33px;">
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>原始密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="mpass" name="mpass" size="50" placeholder="请输入原始密码"/>
                    <span id="checkPW"></span>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>新密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="newpass" name="newpass" size="50"
                           placeholder="请输入新密码"/>
                    <span id="checkPWNew"></span>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>确认新密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="renewpass" name="renewpass" size="50"
                           placeholder="请再次输入新密码"/>
                    <span id="checkPWReNew"></span>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <input id="submit" class="button bg-main icon-check-square-o" type="button" value="提交">
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    function initPage() {
        var userInfo = $.cookie("userInfo");
        userInfo = JSON.parse(userInfo);
        let userName = userInfo.t.userName;
        $("#adminLoginName").text(userName)
    }

    function verifyHtmlPassword() {
        var regex = /^[0-9a-zA-Z_]{6,9}$/
        let mpass = $("#mpass").val();
        if(regex.test(mpass)){
            $("#checkPW").text("");
            $("#mpass").css("border","1px solid green")
            return true;
        }else{
            $("#checkPW").text("密码由数字、字母、_组成，长度必须是6~9位")
            $("#checkPW").css("color","red");
            $("#mpass").css("border","1px solid red")
            return false;
        }

    }

    function verifyServerPassword() {
        var data = {"userName":$("#adminLoginName").text(), "password":$("#mpass").val()}
        $.ajax({
            "url":"/user/verifyPwd",
            "type":"post",
            "data":JSON.stringify(data),
            "dataType":"json",
            "contentType":"application/json; charset=utf-8",
            "success":function (resp){
                if(resp.code!=0){
                    $("#checkPW").text(resp.msg)
                    $("#checkPW").css("color","red");
                    $("#mpass").css("border","1px solid red")
                    result2 = false;
                }else{
                    $("#checkPW").text("");
                    $("#mpass").css("border","1px solid green")
                    result2 = true;
                }
            }
        })
    }

    function verifyHtmlNewPassword() {
        var regex = /^[0-9a-zA-Z_]{6,9}$/
        let newpass = $("#newpass").val();
        if(regex.test(newpass)){
            $("#checkPWNew").text("");
            $("#newpass").css("border","1px solid green")
            return true;
        }else{
            $("#checkPWNew").text("密码由数字、字母、_组成，长度必须是6~9位")
            $("#checkPWNew").css("color","red");
            $("#newpass").css("border","1px solid red")
            return false;
        }
    }

    function verifyHtmlReNewPassword() {
        if( $("#newpass").val() != $("#renewpass").val()){
            $("#checkPWReNew").text("密码不一致")
            $("#checkPWReNew").css("color","red");
            $("#renewpass").css("border","1px solid red")
            return false;
        }else{
            $("#checkPWReNew").text("");
            $("#renewpass").css("border","1px solid green")
            return true;
        }
    }

    var result2;
    $(function () {
        initPage();
        var result1;
        var result3;
        var result4;

        $("#mpass").blur(function (){
            result1 = verifyHtmlPassword();
            if(result1){
                verifyServerPassword();
            }
        })

        $("#newpass").blur(function (){
            result3 = verifyHtmlNewPassword();
        })

        $("#renewpass").blur(function (){
            result4 = verifyHtmlReNewPassword();
        })

        $("#submit").click(function (){
            if(result1 && result2 && result3 && result4){
                var data = {"userName":$("#adminLoginName").text(), "password":$("#newpass").val()}
                $.ajax({
                    "url":"/user/updatePwd",
                    "type":"post",
                    "data":JSON.stringify(data),
                    "dataType":"json",
                    "success":function (resp) {
                        if(resp.code == 0){
                            top.location.href = "/html/login.html"
                        }else{
                            alert(resp.msg)
                        }
                    }
                })
            }else {
                alert("瞎点啥提交！")
            }
        })
    })
</script>
</body>
</html>